<?php

namespace app\api\service;
use app\common\model\Order;
use app\common\model\Parameter;
use app\common\model\User;
use app\common\model\UserAmountRewardDetailed;
use app\Response;

class InviteService
{
    /*** 邀请奖励页面
    */
    public static function inviteViewService($user_id){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[22,23])){
            return Response::wrapError(0,"账户非用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $info = [];
        $ua_where[] = ["uid","=",$user_id];
        $ua_where[] = ["state","=",2];
        $ua_where[] = ["type","in","1,2,3"];
        $userAmountCount = UserAmountRewardDetailed::orderRewardSum($ua_where,"amount_money");
        $info["invite_reward_money"] = $userAmountCount;
        $info["invite_wiseman_num"] = User::dataCount(["superor_id"=>$user_id,"enable"=>1,"type"=>23]);
        $info["invite_merchant_num"] = User::dataCount(["superor_id"=>$user_id,"enable"=>1,"type"=>22]);
        $info["expert_rebate"] = Parameter::parameterValue(["id"=>2]);
        $info["merchant_rebate"] = Parameter::parameterValue(["id"=>3]);
        return Response::wrapData($info);
    }

    /*** 邀请用户列表
     */
    public static function intviteUserListService($user_id,$page,$num,$type){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[21,22,23])){
            return Response::wrapError(0,"账户非用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $list = User::inviteUserList(["u.superor_id"=>$user_id,"u.enable"=>1,"u.type"=>$type],$page,$num,"u.*,ua.accumulated_amount");
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $k => $v){
                if(empty($v["nickname"])){
                    $list[$k]["nickname"] = substr_replace($v['phone'],'****',3,4);
                }
                $list[$k]["addtime"] = !empty($v["addtime"])?date("Y-m-d H:i:s",$v["addtime"]):"";
            }
            $count = User::inviteUserListCount(["u.superor_id"=>$user_id,"u.enable"=>1,"u.type"=>$type]);
        }else{
            $list = [];
            $count = 0;
        }
        return Response::wrapData($list,$count);
    }


    public static function intviteInfoCountService($user_id){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[21,22,23])){
            return Response::wrapError(0,"账户非用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $info = [];
        $user_count = User::inviteUserListCount(["u.superor_id"=>$user_id,"u.enable"=>1]);
        $accumulated_amount = User::inviteUserOrderAmountSum(["u.superor_id"=>$user_id,"u.enable"=>1]);
        $amount_money = UserAmountRewardDetailed::orderRewardSum(["uid"=>$user_id,"state"=>2],"amount_money");
        $timestamp = time();
        // 获取月初时间戳
        $firstDayOfMonth = strtotime(date('Y-m-01 00:00:00', $timestamp));
        // 获取月尾时间戳
        $lastDayOfMonth = strtotime(date('Y-m-t 23:59:59', $timestamp));
        $where[] = ["u.superor_id","=",$user_id];
        $where[] = ["u.enable","=",1];
        $where[] = ["u.lasttime",">",$firstDayOfMonth];
        $where[] = ["u.lasttime","<",$lastDayOfMonth];
        $active_users = User::inviteUserListCount($where);
        $info["user_count"] = $user_count;
        $info["accumulated_amount"] = $accumulated_amount;
        $info["amount_money"] = $amount_money;
        $info["active_users"] = $active_users;
        $info["commission_rate"] = $userInfo["commission_rate"];
        $sum_where[] = ["sup_uid","=",$user_id];
        $sum_where[] = ["enable","=",1];
        $sum_where[] = ["state","<>",0];
        $sum_where[] = ["com_state","=",1];
        $sum = Order::orderSum($sum_where,"commission_info->sup_commission_price");
        $info["next_order_reward_sum"] = number_format($sum,2);
        return Response::wrapData($info);
    }

    /*** 邀请奖励明细
     */
    public static function intviteRewardListService($user_id,$page,$num,$type){
        $userInfo = User::userInfo(["uid"=>$user_id]);
        if(!in_array($userInfo['type'],[22,23])){
            return Response::wrapError(0,"账户非用户!");
        }
        if($userInfo['enable'] == 0){
            return Response::wrapError(-200,"此账户已被禁用,如有疑问请联系平台客服!");
        }
        $where[] = ["ua.uid","=",$user_id];
        if(!empty($type)){
            $where[] = ["u.type","=",$type];
        }
        $list = UserAmountRewardDetailed::rewardDetailsList($page,$num,$where,"ua.*,u.nickname,u.phone");
        if(!empty($list)){
            $list = $list->toArray();
            $count = UserAmountRewardDetailed::rewardDetailsCount($where,"ua.*,u.nickname,u.phone");
            foreach ($list as $k => $v){
                if(empty($v["nickname"])){
                    $list[$k]["nickname"] = substr_replace($v['phone'],'****',3,4);
                }
                $list[$k]["addtime"] = !empty($v["addtime"])?date("Y-m-d H:i:s",$v["addtime"]):"";
            }
        }else{
            $count = 0;
            $list = [];
        }
        return Response::wrapData($list,$count);
    }
}